import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "element-plus/dist/index.css";
import { ElMessage as $msg } from "element-plus";
import Cookies from "js-cookie"; //引入cookie
import "default-passive-events"; //解决没有添加被动事件监听器来阻止'touchstart'事件

// 全局配置
import axios from "axios";
import VueAxios from "vue-axios";

// 国际化
import ElementPlus from "element-plus";
import zhCn from "element-plus/es/locale/lang/zh-cn";

// 进度条
import NProgress from "nprogress";
import "nprogress/nprogress.css";

// 时间
require("@/utils/DateFormatter/DateFormatter");

// 路由守卫
router.beforeEach((to, from, next) => {
  //页面跳转之前，开启进度条
  NProgress.start();
  if (Cookies.get("token") || to.path == "/Login" || to.path == "/404") {
    next();
  } else if (!sessionStorage.getItem("token")) {
    $msg.warning("请先登录");
    sessionStorage.removeItem("token");
    next({
      path: "/Login",
      query: { namea: to.path },
    });
  } else {
    $msg.warning("身份过期请重新登录");
    sessionStorage.removeItem("token");
    next({
      path: "/Login",
      query: { namea: to.path },
    });
  }
});

// 路由结束
router.afterEach((to) => {
  NProgress.done();
  window.scrollTo(0, 0);
});

const app = createApp(App);
app
  .use(store)
  .use(ElementPlus, {
    locale: zhCn,
  })
  .use(router)
  .use(VueAxios, axios)
  .mount("#app");

// 自定义指令
app.directive("permission", {
  mounted(el, binding) {
    let val = binding.value;
    var permissionList = store.getters.permissionList || [];
    if (permissionList.indexOf(val) === -1) {
      //没有权限
      el.remove();
    }
  },
});
